package com.azirkxs.clothes.cys.controller;

import com.azirkxs.clothes.common.vo.Result;
import com.azirkxs.clothes.common.vo.UserPwd;
import com.azirkxs.clothes.cys.entity.User;
import com.azirkxs.clothes.cys.service.IUserService;
import com.azirkxs.clothes.cys.service.impl.BusinessException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.stereotype.Controller;

import java.util.List;
import java.util.Map;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author azirkxs
 * @since 2023-04-09
 */
@RestController
@RequestMapping("/user")
public class UserController {
    @Autowired
    private IUserService userService;

    @GetMapping("/all")
    public Result<List<User>> getAllUser(){
        List<User> list = userService.list();
        return Result.success(list,"查询成功");
    }

    @PostMapping("/login")
    public Result<Map<String,Object>> login(@RequestBody User user){
        Map<String, Object> data = userService.login(user);
        if(data!=null){
            return Result.success(data);
        }

        return Result.fail(20002,"用户名或密码错误");
    }

    @GetMapping("/info")
    public Result<Map<String,Object>> getUserInfo(@RequestParam("token")String token){
        //根据token来获取用户信息,redis
        Map<String,Object> data = userService.getUserInfo(token);
        if(data!=null){
            return Result.success(data);
        }

        return Result.fail(20003,"登录信息无效，请重新登录");
    }

    @PostMapping("/logout")
    public Result<Map<String,Object>> logout(@RequestHeader("X-Token") String token){
        userService.logout(token);
        return Result.success();
    }

    @PostMapping("/updateUserInfo")
    public Result<Map<String,Object>> updateUserInfo(@RequestParam("token")String token){
        //根据token来获取用户信息,redis
        Map<String,Object> data = userService.getUserInfo(token);
        if(data!=null){
            return Result.success(data);
        }

        return Result.fail(20003,"登录信息无效，请重新登录");
    }

    @GetMapping("/personal")
    public Result<Map<String,Object>> personal(@RequestParam("token") String token){
        Map<String,Object> data = userService.getPersonalReocommend(token);
        if(data!=null){
            return Result.success(data);
        }

        return Result.fail(20003,"登录信息无效，请重新登录");
    }

    @PostMapping("/register")
    public Result<Map<String,Object>> register(@RequestBody User user) {
        try {
            userService.register(user);
            return Result.success();
        } catch (BusinessException e) {
            return Result.fail(20003,"用户名已存在");
        }
    }

    @PostMapping("/editInfo")
    public Result<Map<String,Object>> editInfo(@RequestBody User user){
        userService.infoEdit(user);

        return Result.success();
    }

    @PostMapping("/changePassword")
    public Result<Map<String,Object>> changePassword(@RequestBody UserPwd userPwd){
        int n = userService.changePassword(userPwd.getNewPassword(),userPwd.getOldPassword(),userPwd.getUserId());
        if(n>0){
            return Result.success();
        }
        return Result.fail("修改失败，请检查");
    }
}
